package com.tjdr.his.drugstore.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.tjdr.his.domain.entity.DrugInfo;
import com.tjdr.his.domain.entity.Prescription;
import com.tjdr.his.domain.entity.Register;
import com.tjdr.his.domain.entity.StorageInfo;
import com.tjdr.his.domain.mapper.IGiveBackRecordsMapper;
import com.tjdr.his.domain.mapper.IPrescriptionMapper;
import com.tjdr.his.domain.mapper.IRegisterMapper;
import com.tjdr.his.drugstore.service.IPrescriptionService;
import com.tjdr.his.drugstore.service.IStorageInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @AUTHOR 张三
 * @DATE 2024/10/16 10:54
 * @DESCRIBER 处方的业务
 */
@Slf4j
@Service
public class IPrescriptionServiceImpl implements IPrescriptionService {
    @Autowired
    private IPrescriptionMapper mapper;

    @Autowired
    private IGiveBackRecordsMapper iGiveBackRecordsMapper;
    @Autowired
    private IRegisterMapper registerMapper;
    @Autowired
    private IStorageInfoService service;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public synchronized boolean givePresById(int id, String targetDrugState, String drugCode, int drugNumber) {

        //去库存表中查看 某药品的库存量
        StorageInfo storageInfo = service.getStorageByDrugCode(drugCode);
        log.debug("库存信息：{}",storageInfo);
        // 如果库存量足够多
        int storageNumber = storageInfo.getStorageNumber();
        if (storageNumber >= drugNumber) {
            int lastStorageNumber = storageNumber - drugNumber;//最终的库存量
            storageInfo.setStorageNumber(lastStorageNumber);
            service.updateStorageById(storageInfo);//修改库存
            // 修改处方的状态
            Prescription prescription = new Prescription();
            prescription.setId(id);
            prescription.setDrugState(Integer.valueOf(targetDrugState));
            int count = mapper.updateById(prescription);
            //
            return count > 0 ? true : false;
        }
        return false;


//        LambdaUpdateWrapper<Prescription> lambda = new UpdateWrapper<Prescription>().lambda();
//        lambda.set(Prescription::getDrugState,targetDrugState);
//        lambda.ge(Prescription::getId,id);
//         int count = mapper.update(lambda);
//        return count>0?true:false;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean repercussionDrugById(int id, String drugCode, int drugNumber) {
        //去库存表中查看 某药品的库存量
        StorageInfo storageInfo = service.getStorageByDrugCode(drugCode);
        //获取之前的库存量
        int storageNumber = storageInfo.getStorageNumber();
        int lastStorageNumber = storageNumber + drugNumber;//最终的库存量
        storageInfo.setStorageNumber(lastStorageNumber);
        service.updateStorageById(storageInfo);//修改库存
        // 修改处方的状态
        Prescription prescription = new Prescription();
        prescription.setId(id);
        prescription.setDrugState(Integer.valueOf("已退药"));
        //更新數據庫
        int count = mapper.updateById(prescription);
        return count > 0 ? true : false;


    }

    @Override
    public IPage getRegisterPresDrug(int id, int pageSize, int currentPage, String drugState) {
        IPage<Map<String, Object>> page = new Page<>();
        page.setCurrent(currentPage);//当前页
        page.setSize(pageSize);//设置每页显示的数量
        MPJLambdaWrapper<Register> mpjLambdaWrapper = new MPJLambdaWrapper();
        mpjLambdaWrapper.eq(Register::getId, Integer.valueOf(id));
        mpjLambdaWrapper.eq(Prescription::getDrugState, drugState);

        mpjLambdaWrapper.selectAs(Register::getId, "register_id");
        mpjLambdaWrapper.select(Register::getEmployeeId);
        mpjLambdaWrapper.select(Prescription::getId);
        mpjLambdaWrapper.selectAll(DrugInfo.class);//要查询出DrugInfo的所有字段
        mpjLambdaWrapper.selectAll(Prescription.class);//要查询出处方的所有字段

        mpjLambdaWrapper.innerJoin(Prescription.class, Prescription::getRegisterId, Register::getId);
        mpjLambdaWrapper.innerJoin(DrugInfo.class, DrugInfo::getId, Prescription::getDrugId);
        return registerMapper.selectJoinMapsPage(page, mpjLambdaWrapper);
    }

}
